package com.rzico.quartz.job;

import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.entity.SysJob;
import com.rzico.core.entity.SysPlugin;
import com.rzico.core.service.SysJobService;
import com.rzico.core.service.SysPluginService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.market.entity.MarketOrderApply;
import com.rzico.market.service.MarketOrderApplyService;
import com.rzico.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 京东订单发票申请
 */
public class JDOrderApplySubmitTask implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MarketOrderApplyService marketOrderApplyService;

    @Autowired @Lazy
    private SysSequenceService sysSequenceService;

    @Autowired
    protected SysPluginService sysPluginService;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {
            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }
            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里开始
            /**
             * 获取当前商户的信息
             */
            String mchId = "";
            Long enterpriseId = 0L;
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("name","葫芦派");
            List<Enterprise> enterpriseList= enterpriseService.selectList(param);
            if (0 < enterpriseList.size()) {
                mchId = enterpriseList.get(0).getMchId();
                enterpriseId = enterpriseList.get(0).getId();
            }
            //根据申请退货单，京东商品申请售后
            SysPlugin sysplugin = sysPluginService.findByPlugin(mchId, "marketJDPlugin");
            String marketId = "wangyue_apply_" + sysSequenceService.generate("marketId");
            /**
             * 获取上个月未开票的所有京东子订单信息
             */
            Map<String, Object> jdOrderParam = new HashMap<String, Object>();
            jdOrderParam.put("state", 0);
            jdOrderParam.put("endOrderCreateDate", DateUtils.getCurrentMonthBegin("yyyy-MM-dd"));
            List<MarketOrderApply> marketOrderApplyList = marketOrderApplyService.selectList(jdOrderParam);
            marketOrderApplyService.invoiceSubmit(marketOrderApplyList, marketId, sysplugin);

            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }
        catch (Exception e) {
            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }

    }
}
